package springboot.bigdata.demo.dao.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springboot.bigdata.demo.dao.UserDao;
import springboot.bigdata.demo.example.UserExample;
import springboot.bigdata.demo.mappers.comm.UserMapper;
import springboot.bigdata.demo.mappers.db0.DB0UserMapper;
import springboot.bigdata.demo.mappers.db1.DB1UserMapper;
import springboot.bigdata.demo.model.User;

import java.util.List;

@Component
public class UserDaoImpl implements UserDao {

    @Autowired
    private DB0UserMapper db0UserMapper;
    @Autowired
    private DB1UserMapper db1UserMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> getUserList(User userPar) {

        PageHelper.startPage(userPar.getPage(), userPar.getRows());
        List<User> userList = userMapper.selectByExample(new UserExample());

        return new PageInfo<>(userList);
    }

    @Override
    public User addUser(User user) {

        if (user.getUserId() %2 == 0){
            db0UserMapper.insertSelective(user);
        }else {
            db1UserMapper.insertSelective(user);
        }

        return user;
    }

    @Override
    public int addUserList(List<User> userList, int dbType) {

        if (CollUtil.isEmpty(userList)){
            return 0;
        }

        if (dbType == 0){
            db0UserMapper.insertSelectiveList(userList);
        }else if (dbType == 1){
            db1UserMapper.insertSelectiveList(userList);
        }

        return 0;
    }
}
